package com.me.study.concurrent.thread;

/**
 * 
 *
 * @author ME
 * @date   2020/6/2
 */
public class TestInterrupt2 {

    public static void main(String[] args) throws Exception {
        Thread t = new Thread(new Worker());
        t.start();

        Thread.sleep(200);
        // t线程正在 sleep，此时调用 interrupt() 会抛出 InterruptedException，并清除 线程的中断状态
        t.interrupt();

        System.out.println("Main thread stopped.");
    }

    public static class Worker implements Runnable {
        public void run() {
            System.out.println("Worker started.");

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                System.out.println("Worker IsInterrupted: " +
                    Thread.currentThread().isInterrupted());
            }

            System.out.println("Worker stopped.");
        }
    }

}
